#!/usr/bin/perl -w

require "PrefixBits.pl";

sub find_prefixes {
	my $INQUISITIVE_HE=shift;

	my ($mask,$prefix);
	my %prefixes;

	foreach $W ('','ו'){
	  foreach $S ('','ה','ש'){
	    foreach $K ('','כש','מש','לכש'){
	      foreach $k ('','כ'){
		foreach $B ('','ב','ה','כ','ל','מ','מה'){
		  next if (!$INQUISITIVE_HE && $S eq 'ה');
		  next if ($k eq 'כ' && $B eq 'ה');
		  $prefix = "$W$S$K$k$B";
		  $mask = $PS_MISC;
		  $mask |= $PS_L if $B =~ m/[בכלמ]$/;
		  $mask |= $PS_B if $prefix =~ m/^ו?ש?ב$/;
		  $mask |= $PS_VERB if ($k eq "" && $B eq "");
		  $mask |= $PS_NONDEF if $B !~ m/ה$/;
		  $prefixes{$prefix} = 0 if !defined $prefixes{$prefix};
		  $prefixes{$prefix} |= $mask;
		}
	      }
	    }
	  }
	}
	foreach $W ('','ו'){
	  foreach $S ('','ה','ש'){
	    foreach $K ('','כש','מש','לכש'){
	      foreach $B ('','ב','ל','מ'){
	      	foreach $k ('','כ'){
		  next if (!$INQUISITIVE_HE && $S eq 'ה');
		  $prefix = "$W$S$K$B$k";
		  $mask = $PS_MISC;
		  $mask |= $PS_L if $B =~ m/[בכלמ]$/;
		  $mask |= $PS_B if $prefix =~ m/^ו?ש?ב$/;
		  $mask |= $PS_VERB if ($k eq "" && $B eq "");
		  $mask |= $PS_NONDEF if $B !~ m/ה$/;
		  $prefixes{$prefix} = 0 if !defined $prefixes{$prefix};
		  $prefixes{$prefix} |= $mask;
		}
	      }
	    }
	  }
	}
	foreach $B ('ב','ל'){
		$prefix = "מ$B";
		$mask = $PS_MISC;
		$mask |= $PS_L if $B =~ m/[בכלמ]$/;
		$mask |= $PS_B if $prefix =~ m/^ו?ש?ב$/;
		$mask |= $PS_NONDEF if $B !~ m/ה$/;
		$prefixes{$prefix} = 0 if !defined $prefixes{$prefix};
		$prefixes{$prefix} |= $mask;
	}

	$prefixes{""} |= $PS_IMPER;
	$prefixes{"ו"} |= $PS_IMPER;

	return %prefixes;
}

print "/* This file is automatically generated by genprefixes.pl.\n";
print "   DO NOT EDIT THIS FILE DIRECTLY!\n */\n\n";

# Prefix list without He Ha-She'ela (but with He Ha-Yedi`a, of course)

my %prefixes = find_prefixes(0);

print "static char *prefixes_noH[]={";
foreach (sort keys %prefixes) {print "\"$_\",\n"}
print "0};\n";

print "static int masks_noH[]={";
foreach (sort keys %prefixes) {print "$prefixes{$_},\n"}
print "-1};\n";

# Output the same thing, but with He Ha-She'ela...

%prefixes = find_prefixes(1);

print "static char *prefixes_H[]={";
foreach (sort keys %prefixes) {print "\"$_\",\n"}
print "0};\n";

print "static int masks_H[]={";
foreach (sort keys %prefixes) {print "$prefixes{$_},\n"}
print "-1};\n";
